#!/bin/sh

#set -e

# Args
OPER1=$1

# Env
ROOT=/root/test
SBIN=/sbin
INSMOD=/sbin/insmod
LSMOD=/sbin/lsmod
CHMOD=chmod
RMMOD=/sbin/rmmod
IPTABLES=iptables-multi

# Command
IPTABLES=/sbin/iptables

build_apps()

{
	pwd
	chmod +x  $IPTABLES
	chmod +x  test_build
	chmod +x test_addr_group

	cp iptables-multi /sbin
	cp test_build $SBIN
	cp test_addr_group $SBIN

	echo "====App Build Complete====>"
}

build_kern()
{
	pwd

	$LSMOD | grep class_core

	if [ "$?" = 0 ]; then
		$RMMOD class_core
		$RMMOD class_core
	fi	
	
	$INSMOD class_core.ko
	$INSMOD addrrlp.ko 
	
	echo "====Kernel Build Complete====>"
}

build_addr_group_env()
{
	##Create group_test from read file iprange
	./test_build group_test

	./test_addr_group -A grp_real -a 1.1.1.1-2.2.2.1 -a 10.50.10.142-10.50.10.200
}

build_ip_tables_env()
{
	##Create iprables
	iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
	iptables -A FORWARD -m srcaddr --src-addr grp_test  -j ACCEPT
	
	iptables -A FORWARD -m srcaddr --src-addr group_real -j ACCEPT
}

ipt_flush_policy()
{

	$IPTABLES -F -t filter
	echo "Empty all policy!!"
}

# Execute

echo $OPER1
if [ $OPER1 = "build" ]; then
	build_apps
	build_kern

	build_addr_group_env
	build_ip_tables_env
fi

#if [ $OPER1 = "add" ]; then
#	
##        if [ "$?" != 0 ]; then
##                echo "Pls insmod xt_ruleid kernel module!!!"
#	else
#		ipt_loop_add leadsec $INDEV $OUTDEV
#        fi
#fi

#if [ $OPER1 = "flush" ]; then
#	ipt_flush_policy
#fi

